var e = require("../../../@babel/runtime/helpers/interopRequireDefault").default, t = require("../../../@babel/runtime/helpers/regeneratorRuntime"), n = require("../../../@babel/runtime/helpers/asyncToGenerator"), i = e(require("../../../api/inviteRecommendPhone.js")), o = e(require("../../../kta/index")), a = require("../../../utils/util"), s = getApp().eventBus;

function r(e) {
    return ("00" + (e = String(e))).substr(e.length);
}

Component({
    properties: {
        showBtn: {
            type: Boolean,
            default: !1
        },
        query: {
            type: Object,
            default: {}
        }
    },
    data: {
        pageShow: !1,
        showDialog: !1,
        finish: !1,
        dialog: {
            src: "/resources/images/inviteRecommendPhone/icon_error.png",
            title: "提示",
            descList: []
        },
        contactNum: "",
        endDate: "",
        options: {},
        phoneNum: "",
        phoneList: [],
        errMsg: "",
        imgList: [ {
            src: "https://txwk.10010.com/static/wkpen/179d4f65d6e3f63e92f.png"
        } ],
        imgList2: [ {
            src: "https://txwk.10010.com/static/wkpen/179d4f65d6f3d6ba798.png"
        }, {
            src: "https://txwk.10010.com/static/wkpen/179d4f65d6a51f6661a.png"
        } ],
        time: 108e6,
        timeData: {}
    },
    methods: {
        init: function() {
            var e = this;
            this.getOptions().then(function() {
                e.getRecommendSelect();
            });
        },
        toChooseNum: function(e) {
            var t = e.currentTarget.dataset.index, n = this.data.phoneList;
            n.forEach(function(e, n) {
                e.act = n === t;
            }), this.setData({
                phoneList: n
            }), this.setActNumber();
        },
        getOptions: function() {
            var e = this;
            return new Promise(function(t, n) {
                e.inviteRecommend_options = s.on("inviteRecommend_options", function(i) {
                    e.setData({
                        options: i
                    }), i.encData || n(), t();
                });
            });
        },
        onChange: function(e) {
            var t = e.detail, n = t.days, i = t.hours, o = t.minutes, a = t.seconds;
            this.setData({
                timeData: {
                    hours: r(24 * n + 1 * i),
                    minutes: r(o),
                    seconds: r(a)
                }
            });
        },
        finished: function() {
            this.setData({
                finish: !0
            });
        },
        onClose: function() {
            this.setData({
                showDialog: !1
            });
        },
        getRecommendSelect: function() {
            var e = this;
            return n(t().mark(function n() {
                var o, s, r, c, h, d, u, m, p, f, l, g, w, D, b, v;
                return t().wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return wx.showLoading({
                            title: "加载中..."
                        }), t.prev = 1, t.next = 4, i.default.recommendSelect(e.data.options);

                      case 4:
                        o = t.sent, s = o.data, c = (r = void 0 === s ? {} : s).contactNum, h = r.phoneNum, 
                        d = r.phoneList, u = r.nowTime, m = r.endTime, d && d.length > 0 && (d.forEach(function(t) {
                            t.showNumber = e.phoneAddGap(t.number);
                        }), d[0].act = !0), e.setData({
                            contactNum: c ? e.phoneAddGap(c) : "",
                            phoneNum: h ? e.phoneAddGap(h) : "",
                            phoneList: d && d.length > 0 ? d : [],
                            time: m - u,
                            endDate: (0, a.dateFormat)(new Date(m), "YYYY年MM月DD日"),
                            finish: m <= u,
                            pageShow: !0
                        }), wx.hideLoading(), e.getBtnTop(), !e.data.finish && e.data.phoneNum || e.showErrorDialog(), 
                        e.setActNumber(), t.next = 29;
                        break;

                      case 20:
                        t.prev = 20, t.t0 = t.catch(1), p = t.t0.message, (f = t.t0.data) || (f = {}), g = (l = f).contactNum, 
                        w = l.phoneNum, D = l.phoneList, b = l.nowTime, v = l.endTime, D && D.length > 0 && (D.forEach(function(t) {
                            t.showNumber = e.phoneAddGap(t.number);
                        }), D[0].act = !0), e.setData({
                            contactNum: g ? e.phoneAddGap(g) : "",
                            phoneNum: w ? e.phoneAddGap(w) : "",
                            phoneList: D && D.length > 0 ? D : [],
                            endDate: v ? (0, a.dateFormat)(new Date(v), "YYYY年MM月DD日") : "",
                            time: v - b,
                            finish: v <= b,
                            pageShow: !0,
                            errMsg: p
                        }), wx.hideLoading(), e.showErrorDialog();

                      case 29:
                      case "end":
                        return t.stop();
                    }
                }, n, null, [ [ 1, 20 ] ]);
            }))();
        },
        setActNumber: function() {
            var e = this.data.phoneList.find(function(e) {
                return e.act;
            });
            e && s.emit("inviteRecommend_getNumber", e.number);
        },
        showErrorDialog: function() {
            this.data.finish ? this.setData({
                showDialog: !0,
                dialog: {
                    src: "/resources/images/inviteRecommendPhone/icon_over.png",
                    title: "活动已结束",
                    descList: [ "活动已结束，感谢您的支持" ]
                }
            }) : this.data.errMsg ? this.setData({
                showDialog: !0,
                dialog: {
                    src: "/resources/images/inviteRecommendPhone/icon_error.png",
                    title: "温馨提示",
                    descList: [ this.data.errMsg ]
                }
            }) : this.setData({
                showDialog: !0,
                dialog: {
                    src: "/resources/images/inviteRecommendPhone/icon_error.png",
                    title: "很遗憾",
                    descList: [ "您已经错过了我们的精选号码", "感谢您的支持" ]
                }
            });
        },
        gotoAppRule: function() {
            this.openWebview("https://txwk.10010.com/kcardorder/rules/index.html#/appRule");
        },
        gotoFeeRule: function() {
            this.openWebview("https://txwk.10010.com/kcardorder/rules/index.html#/feeRule");
        },
        openWebview: function(e) {
            wx.navigateTo({
                url: "/pages/webview/index?url=" + encodeURIComponent(e)
            });
        },
        clickInv: function() {
            o.default.clickStat("67734369d5a14c588a5f59643b4c60a6", {});
        },
        gotoOrder: function() {
            if (o.default.clickStat("fb50bae936bb4f13a458ad22690874ad", {}), this.data.finish || !this.data.phoneNum || this.data.errMsg) this.showErrorDialog(); else {
                var e = this.data.query, t = e.encData, n = e.channel, i = void 0 === n ? "01-3535-9999-9999" : n, s = this.data.phoneList.find(function(e) {
                    return e.act;
                }), r = "".concat("https://txwk.10010.com", "/kcardorder/gotoUid?state=KCARD_INVITE_RECOMMEND2_ORDERLAND&isSelf=1&").concat((0, 
                a.objToStr)({
                    encData: t,
                    channel: i,
                    ADTAG: i,
                    actNumber: s.number
                }));
                console.log(r), this.openWebview(r);
            }
        },
        getBtnTop: function() {
            wx.createSelectorQuery().in(this).select("#btn-wrap").boundingClientRect(function(e) {
                e && s.emit("inviteRecommend_getBtnTop", e.top);
            }).exec();
        },
        phoneAddGap: function(e) {
            var t = e.replace(/(\d{3})(\d{4})/, "$1 $2 ");
            return this.formatPhone(t, 9, 4);
        },
        formatPhone: function(e, t, n) {
            return [ e.substring(0, t), e.substring(t, t + n) ];
        }
    },
    created: function() {},
    attached: function() {
        this.init();
    },
    ready: function() {},
    moved: function() {},
    detached: function() {
        this.inviteRecommend_options && this.inviteRecommend_options.off();
    }
});